package com.lewis.module.m2.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lewis.api.system.RemoteRecordConfigFunctionService;
import com.lewis.api.system.entity.BotConfigFunction;
import com.lewis.api.system.entity.BotQq;
import com.lewis.api.system.vo.GetOneVO;
import com.lewis.common.core.utils.SpringUtil;
import com.lewis.module.m2.PluginMain;
import lombok.extern.slf4j.Slf4j;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.BotFactory;
import net.mamoe.mirai.console.extension.PluginComponentStorage;
import net.mamoe.mirai.console.terminal.MiraiConsoleImplementationTerminal;
import net.mamoe.mirai.console.terminal.MiraiConsoleTerminalLoader;
import net.mamoe.mirai.message.data.*;
import net.mamoe.mirai.utils.BotConfiguration;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author Lewis - 398529803@qq.com
 * @date 2021年11月30日, 030 - 17:07:46
 */
@Slf4j
public class BotUtils {

	/**
	 * 存放bot列表
	 */
	public static List<Bot> botList = new ArrayList<>();

	/**
	 * mirai框架启动
	 */
	public static void botStart() {
		log.info("启动机器人框架......");
		// 启动时 删除缓存 减少 程序启动后堵塞无法登录的情况
		CacheUtils.delete();
		// 启动console 加载插件
		MiraiConsoleTerminalLoader.INSTANCE.startAsDaemon(new MiraiConsoleImplementationTerminal());
		// 实例化机器人自带插件
		PluginMain.INSTANCE.onLoad(new PluginComponentStorage(PluginMain.INSTANCE));
		PluginMain.INSTANCE.onEnable();
	}

	/**
	 * 初始化机器人实体
	 *
	 * @param qq
	 * @param password
	 */
	public static void init(Long qq, String password) {
		// 创建 Bot 机器人实体
		Bot bot = BotFactory.INSTANCE.newBot(qq, password, getBotConfig(qq));
		// 添加进机器人列表
		botList.add(bot);
		// 登录
		bot.login();
	}

	/**
	 * 机器人默认设置
	 *
	 * @return BotConfiguration
	 */
	private static BotConfiguration getBotConfig(Long qq) {
		BotConfiguration botConfiguration = new BotConfiguration();
		// 登录协议
		botConfiguration.setProtocol(BotConfiguration.MiraiProtocol.ANDROID_PHONE);
		// 心跳协议
		botConfiguration.setHeartbeatStrategy(BotConfiguration.HeartbeatStrategy.STAT_HB);
		// 设置 cache 目录
		botConfiguration.setCacheDir(new File(System.getProperty("user.dir") + "/cache/cache-" + qq));
		// 设置 device
		botConfiguration.fileBasedDeviceInfo(System.getProperty("user.dir") + "/device/device-" + qq + ".json");
		// 设置是否掉线重登录
		botConfiguration.setAutoReconnectOnForceOffline(true);
		return botConfiguration;
	}

	/**
	 * 发送指定信息给指定好友
	 *
	 * @param botQq 机器人QQ
	 * @param qq  指定qq
	 * @param msg 指定信息eq
	 */
	public static void send2friend(long botQq, long qq, MessageChain msg) {
		botList.stream().filter(bot -> bot.getId() == botQq).forEach(bot -> Objects.requireNonNull(bot.getFriend(qq)).sendMessage(msg));
		// MessageChainBuilder builder = new MessageChainBuilder();
		// for (SingleMessage singleMessage : msg) {
		// 	builder.append(singleMessage instanceof Image ? (Image) singleMessage : singleMessage);
		// }
		// Objects.requireNonNull(bot.getFriend(qq)).sendMessage(builder.build());
	}

	/**
	 * 发送指定信息给指定好友
	 *
	 * @param botQq 机器人QQ
	 * @param qq  指定qq
	 * @param msg 指定信息
	 */
	public static void send2friend(long botQq, long qq, ForwardMessage msg) {
		botList.stream().filter(bot -> bot.getId() == botQq).forEach(bot -> Objects.requireNonNull(bot.getFriend(qq)).sendMessage(msg));
		// MessageChainBuilder builder = new MessageChainBuilder();
		// for (SingleMessage singleMessage : msg) {
		// 	builder.append(singleMessage instanceof Image ? (Image) singleMessage : singleMessage);
		// }
		// Objects.requireNonNull(bot.getFriend(qq)).sendMessage(builder.build());
	}

	/**
	 * 发送指定信息给指定群组
	 *
	 * @param botQq 机器人QQ
	 * @param group  指定qq
	 * @param msg 指定信息eq
	 */
	public static void send2group(long botQq, long group, MessageChain msg) {
		botList.stream().filter(bot -> bot.getId() == botQq).forEach(bot -> Objects.requireNonNull(bot.getGroup(group)).sendMessage(msg));
		// MessageChainBuilder builder = new MessageChainBuilder();
		// for (SingleMessage singleMessage : msg) {
		// 	builder.append(singleMessage instanceof Image ? (Image) singleMessage : singleMessage);
		// }
		// Objects.requireNonNull(bot.getFriend(qq)).sendMessage(builder.build());
	}

	/**
	 * 删除指定好友列表
	 *
	 * @param botQq 机器人QQ
	 * @param qqs  指定qq列表
	 */
	public static void deleteFriend(Long botQq, Long[] qqs) {
		for (Bot bot : botList) {
			if (bot.getId() == botQq) {
				for (Long qq : qqs) {
					Objects.requireNonNull(bot.getFriend(qq)).delete();
				}
			}
		}
	}

	/**
	 * 发送指定信息给指定好友
	 *
	 * @param group  	指定群
	 * @param qq  		指定qq
	 * @param msg 		指定信息
	 */
	public static void send2groupMember(long botQq, long group, long qq, MessageChain msg) {
		botList.stream().filter(bot -> bot.getId() == botQq).forEach(bot -> Objects.requireNonNull(Objects.requireNonNull(bot.getGroup(group)).get(qq)).sendMessage(msg));
	}
}
